package com.rabbits.ui.dialog.custom;

import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.os.Bundle;
import android.support.annotation.NonNull;

import com.facebook.react.ReactRootView;
import com.rabbits.ui.R;

public class JsCustomDialog extends Dialog implements DialogInterface.OnCancelListener {
    private Context mCtx = null;

    private OnCancelListener listener = null;
    private ReactRootView reactRootView = null;
    private boolean cancelable = false;

    public JsCustomDialog(@NonNull Context context, Builder builder) {
        super(context, R.style.CommonDialog);
        this.mCtx = context;
        this.listener = builder.listener;
        this.cancelable = builder.cancelable;
        this.reactRootView = builder.reactRootView;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(this.reactRootView);
        setCancelable(this.cancelable);
        setCanceledOnTouchOutside(this.cancelable);
        this.setOnCancelListener(this);
    }

    @Override
    public void onCancel(DialogInterface dialog) {
        if (this.listener != null) {
            this.listener.onCancel();
        }
    }

    // **************************************************************************
    // ===== 构造器 =====
    // **************************************************************************
    public static class Builder {
        private boolean cancelable = true;
        private ReactRootView reactRootView = null;
        private OnCancelListener listener = null;

        /**
         * 设置加载提示文本
         */
        public Builder setReactView(ReactRootView reactRootView) {
            this.reactRootView = reactRootView;
            return this;
        }

        public Builder onCancel(OnCancelListener listener) {
            this.listener = listener;
            return this;
        }

        public Builder cancelable(boolean cancelable) {
            this.cancelable = cancelable;
            return this;
        }

        public JsCustomDialog build(Context ctx) {
            return new JsCustomDialog(ctx, this);
        }
    }

    // **************************************************************************
    // ===== 事件监听器 =====
    // **************************************************************************
    public interface OnCancelListener {
        void onCancel();
    }

}
